import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '../components/home.vue'
Vue.use(VueRouter)



//解决 在使用ElementUI中的导航时，默认情况下如果重复点击某选项，会报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const routes = [{
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: home,
    // 二级漏油
    children: [

      // 欢迎页
      {
        path: '/home',
        component: () => import('../components/welcome.vue')
      },
      // 用户列表
      {
        path: '/users',
        component: () => import('../views/main/users.vue')
      },
      // 角色列表
      {
        path: '/roles',
        component: () => import('../views/main/roles.vue')
      },
      // 权限列表
      {
        path: '/rights',
        component: () => import('../views/main/rights.vue')
      },
      // 商品列表
      {
        path: '/goods',
        component: () => import('../views/main/goods.vue')
      },
      // 添加商品
      {
        path: "/goods/add",
        component: () => import('../views/main/addGoods.vue')
      },
      // 分类参数
      {
        path: '/params',
        component: () => import('../views/main/params.vue')
      },
      // 商品分类
      {
        path: '/categories',
        component: () => import('../views/main/categories.vue')
      },
      // 订单列表
      {
        path: '/orders',
        component: () => import('../views/main/orders.vue')
      },
      // 数据报表
      {
        path: '/reports',
        component: () => import('../views/main/reports.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  }
]

const router = new VueRouter({
  routes
})





// 添加导航卫士
router.beforeEach((to, from, next) => {
  // to:  到哪里去
  // from: 从哪里来
  // next： 出现时。意味：放行  或者next('url'):相当于重定向
  next()
  // if (to.path == '/home') {
  //   sessionStorage.setItem("defaultActive", '');
  // }
  // 判断去的地址是否为登录页， 是则直接放行
  if (to.path == '/login') return next()
  // 查看本地是否有token值---没有则跳转登录页
  const tokenStr = localStorage.getItem('token_ds')
  if (!tokenStr) {
    Vue.prototype.$alert('登录超时,请重新登录', '登录验证', {
      confirmButtonText: '确定',
      callback: () => {
        return next('/login')
      }
    });
    next()
  }

})

export default router
